package com.lixinlei.springboot_servlet.controller.argumentsresolver.RequestBody;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "requestBodyFilter", urlPatterns = "/request/body/*")
public class RequestBodyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("-------------------RequestBodyFilter.1");

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        MyRequestWrapper requestWrapper = new MyRequestWrapper(httpServletRequest);
        String body = requestWrapper.getBody();
        chain.doFilter(requestWrapper != null ? requestWrapper : request, response);

//        String requestBody = ReadAsChars((HttpServletRequest)request);
//        String requestBody = ReadAsChars2((HttpServletRequest)request);
//        String requestBody = new String(RequestReadUtils.readAsBytes((HttpServletRequest)request));
//        chain.doFilter(request, response);

        System.out.println("-------------------RequestBodyFilter.2");
    }



}
